<?php
// /api_crawler
class Api_CrawlerController extends D_Controller {
	private $_top;
	public $cralwer_obj;
	public $website_obj;
	public $crawl_website_url_obj;
	public $keyword_obj;
	public $data_obj;
	public $page_obj;
	
	function init() {
		set_time_limit(8889889);
		$this->setLayout ('main', '' );
		$this->cralwer_obj = new Crawler();
		$this->website_obj = new Website();
		$this->crawl_website_url_obj = new CrawlerWebsiteUrl();
		$this->keyword_obj = new Keyword();
		$this->data_obj = new Data();
		$this->page_obj = new Page();
	}
	
	
	function indexAction(){
		$this->noView();
		echo "ss";
	}
	
	function autoAction(){
		$this->noView();
		//if(SystemConfig::stop_crawler()){
		//	echo "Crawler system is stopped!";
		//	exit;
		//}
			
		if($this->_request->isPost()):
			//create new crawler
			$crawler_id = $this->cralwer_obj->autocrawler();
			$auto_crawl = new AutoCrawl();
			$auto_crawl->run($crawler_id);
			
			//create keyword ranking 
			
			if(SystemConfig::stop_crawler()){
				echo "Crawler system is stopped!";
				exit;
			}
			$keyword_ranking_history_obj = new KeywordRankingHistory();
			$keyword_ranking_history_obj->create_ranking(array("crawler_id" => $crawler_id));
			
			//finish crawler
			$this->cralwer_obj->finish($crawler_id);
			echo "Finished!";
			return;
			
		endif;
	}
	
	function geturlAction(){
		$this->noView();
		$id = $this->_request->getParam("id");
		$crawl = new Crawl();
		$crawl->start($id);
		$ret = $crawl->crawl_all_website($id);
		if($ret){
			echo "Get URL Of Wesites successful";
		}else 
			echo "Get URL Of Wesites unsuccessful";
	}
	
	function getkwAction(){
		$this->noView();
		$id = $this->_request->getParam("id");
		$crawl = new Crawl();
		$ret = $crawl->getkw($id);
		//if(empty($ret)){
		//	echo "Get KW Of URL Of Wesites successful";
		//}else
		//	echo "Get KW Of URL Of Wesites unsuccessful";
		
		echo "</br>";
		echo "successful";
		
		//update finish crawler
		$crawl->finish($id);
		
	}
	
	function websitesAction(){
		$this->noLayout();
		$id = $this->_request->getParam("id");
		$websites = $this->cralwer_obj->list_website($id);
		$this->view->websites = $websites;
	}
	
	function updatekwrankingAction(){
		$this->noView();
		$id = $this->_request->getParam("id");
		$name = "Ranking For Crawler:".$id;
		$krh = new KeywordRankingHistory();
		$history_id = $krh->create_history($name);
		$str = $krh->create_ranking(array('crawler_id'=>$id, 'history_id' => $history_id));
		echo $str;
	}
	
	function updatewebsiterankingAction(){
		$this->noView();
		$id = $this->_request->getParam("id");
		$name = "Ranking For Crawler:".$id;
		echo $name;
		$wrh = new WebsiteRankingHistory();
		$history_id = $wrh->create_history($name);
		$wrh->create_ranking(array('crawler_id'=>$id, 'history_id' => $history_id));
	}
	
	//caller: file:common.js  funciton:crawler_update_category_website_ranking
	function updatecategorywebsiterankingAction(){
		$this->noView();
		$id = $this->_request->getParam("id");
		$name = "Ranking For Crawler:".$id;
		echo $name;
		$wrh = new CategoryWebsiteRankingHistory();
		$history_id = $wrh->create_history($name);
		$wrh->create_ranking(array('crawler_id'=>$id, 'history_id' => $history_id));
	}
	
	
	
}

?>